﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace sortSample
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        Label[] lbs;
        int i = 0, j = 0;
        private void button1_Click(object sender, EventArgs e)
        {
            this.Enabled = false;
            if (lbs?.Length > 0)
            {   
                foreach (Label l in lbs)
                {
                    l.Dispose();
                }
            }
            string[] nums = textBox1.Text.Split(',');
            lbs = new Label[nums.Length];
            int last = 15;
            for (int i = 0; i < nums.Length; i++)
            {
                lbs[i] = new Label();
                lbs[i].AutoSize = true;
                lbs[i].Font = new Font("宋体", 20);
                lbs[i].Text = nums[i];
                lbs[i].Top = textBox1.Bottom + 50;
                lbs[i].Left = last;
                last = last+ lbs[i].Width;
                Controls.Add(lbs[i]);
            }

            if (radioButton1.Checked)
            {
                SelectionSort();
            }
            else if (radioButton2.Checked)
            {
                MaopaoSort();
            }
            else if (radioButton3.Checked)
            {
                InsertionSort();
            }

            Enabled= true;
        }
        private void swap(ref Label l1, ref Label l2)
        {
            label2.Text = l1.Text + " 比 " + l2.Text + " 大，交换位置 ";
            Refresh();
            Thread.Sleep(1000);
            for (int a = 0; a < 10; a++)
            {
                l1.Top += 5;
                l2.Top += 8;
                Refresh();
                Thread.Sleep(100);
            }
            int cha = l2.Left - l1.Left; 
            int mil=cha/50;
            mil = mil > 1 ? mil : 1;
            for (int b = 0; b < cha; b+=mil)
            {
                l1.Left += mil;
                l2.Left -= mil;
                Refresh();
               
                //Thread.Sleep(1);
            }
            for (int a = 0; a < 10; a++)
            {
                l1.Top -= 5;
                l2.Top -= 8;
                Refresh();
                Thread.Sleep(100);
            }
            string text;
            Label temp;

            temp = l1;
            l1 = l2;
            l2 = temp;

            //text = l1.Text;
            //l1.Text = l2.Text;
            //l2.Text = text;
            label2.Text = "";
            Refresh();
            Thread.Sleep(500);
        }
        private void SelectionSort()
        {
            for (i = 0; i < lbs.Length - 1; i++)
            {
                for (j = i + 1; j < lbs.Length; j++)
                {
                    lbs[i].BackColor = Color.Red;
                    lbs[j].BackColor = Color.Red;
                    int a = int.Parse(lbs[i].Text);
                    int b = int.Parse(lbs[j].Text);
                    if (a > b)
                    {
                        swap(ref lbs[i], ref lbs[j]);
                    }
                    else
                    {
                        label2.Text = lbs[i].Text + " 比 " + lbs[j].Text + " 小，不交换位置";
                        Refresh();
                        Thread.Sleep(1000);
                    }
                    lbs[i].BackColor = Color.White;
                    lbs[j].BackColor = Color.White;
                }
            }
        }

        private void MaopaoSort()
        {
            for (i = lbs.Length - 1; i > 0; i--)
            {
                for (j = 0; j < i; j++)
                {
                    lbs[j].BackColor = Color.Red;
                    lbs[j + 1].BackColor = Color.Red;
                    int a = int.Parse(lbs[j].Text);
                    int b = int.Parse(lbs[j + 1].Text);
                    if (a > b)
                    {
                        swap(ref lbs[j], ref lbs[j + 1]);
                    }
                    else
                    {
                        label2.Text = lbs[j].Text + " 比 " + lbs[j + 1].Text + " 小，不交换位置";
                        Refresh();
                        Thread.Sleep(1000);
                    }
                    lbs[j].BackColor = Color.White;
                    lbs[j + 1].BackColor = Color.White;
                }
            }
        }

        private void InsertionSort()
        {
            label2.Text = "";
            for (int a = 0; a < lbs.Length; a++)
            {
                lbs[a].BackColor = Color.Red;
                lbs[a].Top += 60;
                lbs[a].Left = 15;
               
                Refresh();
                Thread.Sleep(1000);
                for (int b = 0; b < a; b++)
                {
                    if (a == 0)
                    {
                        break;
                    }

                    if (int.Parse(lbs[b].Text) > int.Parse(lbs[a].Text))
                    {
                        lbs[b].Left += lbs[a].Width;
                       
                    }
                    else
                    {
                        lbs[a].Left += lbs[b].Width;
                    }
                    Refresh();
                    Thread.Sleep(1000);

                }
                lbs[a].Top += 40;
                Refresh();
            }
        }
    }
}
